package com.dongan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongan.mapper.PermissionMapper;
import com.dongan.mapper.UsersMapper;
import com.dongan.pojo.Permission;
import com.dongan.pojo.PermissionRole;
import com.dongan.pojo.Roles;
import com.dongan.pojo.Users;
import com.dongan.service.PermissionRoleService;
import com.dongan.service.PermissionService;
import com.dongan.utils.CommonProp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author longTao
 * @since 2021-06-15
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {


    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private PermissionRoleService permissionRoleService;
    @Override
    public List<Permission> getList() {
        return recursiveQueryPermissionLevel(permissionMapper.selectList(null));
    }

    /**
     * 根据角色id获取它的权限id
     *
     * @param list
     * @return
     */
    @Override
    public List<Long> getListByRoleId(List<Roles> list) {
        List<Long> longs= usersMapper.queryPermissionLong(list);
        Set<Long> set=new HashSet<>(longs);
        List<Long> longList=new ArrayList<>(set);
        return longList;
    }

    /**
     * @param roleId    角色id,根据角色删除
     * @param roleArray 权限数据,添加角色和权限的关系
     */
    @Override
    @Transactional
    public void rightsProfile(Long roleId, Long[] roleArray) {
        QueryWrapper<PermissionRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        permissionRoleService.remove(queryWrapper);
        List<PermissionRole> list=new ArrayList<>();
        for (Long aLong : roleArray) {
            PermissionRole permissionRole=new PermissionRole();
            permissionRole.setRoleId(roleId);
            permissionRole.setPermissionId(aLong);
            list.add(permissionRole);
        }
        permissionRoleService.saveBatch(list);
        Users users =(Users) CommonProp.user;
        CommonProp.permission=permissionMapper.queryPermissionList(users.getId());
    }

    /**
     * 递归查询权限
     * @param list
     * @return
     */
    public List<Permission> recursiveQueryPermissionLevel(List<Permission> list){
        List<Permission> permissionList=new ArrayList<>();
        for (Permission permission : list) {
            if (permission.getPId()==0){
                permissionList.add(getPermission(list,permission));
            }
        }
        return permissionList;
    }

    /**
     * 查询子权限
     * @param list
     * @param permission
     * @return
     */
    public Permission getPermission(List<Permission> list,Permission permission){
        for (Permission p : list) {
            if(permission.getId().longValue()==p.getPId().longValue()){
                if (permission.getList()==null){
                    permission.setList(new ArrayList<>());
                }
                permission.getList().add(getPermission(list,p));
            }
        }
        return permission;
    }
}
